/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-09-12
 * Time: 20:43
 */


  class ListNode {
      int val;
      ListNode next;
      ListNode() {}
     ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null) {
            if (fast.val != val) {
                slow = fast;
                fast = fast.next;
            }else {
                fast = fast.next;
                slow.next = fast;
            }
        }
        if (head.val == val) {
            head = head.next;
        }

        return head;
    }
}
